<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/12/18
 * Time: 17:33
 */

namespace app\utils;

use Exception;
use OSS\Core\OssException;
use OSS\OssClient;

/**
 * 阿里云上传工具类
 *
 * @package App\Utils
 */
class AliOssUtil
{
    /**
     * 采用二进制上传文件
     *
     * @param $dirName string 文件路径和文件名
     * @param $cont string 二进制文件
     * @return string
     * @throws Exception
     */
    public static function putObject($dirName, $cont)
    {
        try {
            $ossClient = new OssClient(
                env('ali_oss.access_key_id'),
                env('ali_oss.access_key_secret'),
                env('ali_oss.bucket_domain'),
                true
            );
            $ossClient->setTimeout(60);       //默认允许上传1分钟
            $ossClient->setConnectTimeout(10); //连接的超时时间10秒
            $data = $ossClient->putObject(env('ali_oss.bucket'), $dirName, $cont);
        } catch (OssException $e) {
            throw new Exception('文件上传失败:' . $e->getMessage());
        }
    }
}
